package com.neelsys.service.adapter;

import java.util.ArrayList;
import java.util.Set;

import org.springframework.data.neo4j.template.Neo4jOperations;

import com.neelsys.domain.ui.JSONGraph;
import com.neelsys.domain.ui.JSONLink;
import com.neelsys.domain.ui.JSONNode;
import com.neelsys.domain.visualization.CourseDesignator;
import com.neelsys.domain.visualization.EntranceTest;
import com.neelsys.domain.visualization.MajorCombination;

public class MajorCombAdapter {

	private Neo4jOperations template;

	private MajorCombination majorComb;

	public MajorCombAdapter(Neo4jOperations template, MajorCombination majorComb) {
		this.majorComb = majorComb;
		this.template = template;
	}
	
	public JSONGraph adaptToJsonGraph(){
		
		JSONGraph jsonGraph=new JSONGraph();
		
		CourseDesignator courseDesignator=template.fetch(majorComb.getCourseDesigntator());
		
		addNodeToGraph(jsonGraph, createNewNode(courseDesignator.getCourseLevel().name(), "L"));
		addNodeToGraph(jsonGraph,createNewNode(majorComb.getMajorDetails().name(),"M"));
		
		addLinkToGraph(jsonGraph, createNewLink(0, 1));
		
		Set<EntranceTest> entraceTest=template.fetch(majorComb.getEntranceTests());
		
		for (EntranceTest entranceTest : entraceTest) {
			addNodeToGraph(jsonGraph,createNewNode(entranceTest.getEntranceTestDetails().name(),"E"));
			addLinkToGraph(jsonGraph, createNewLink(1, 2));
			
			Set<CourseDesignator> nextLvlCourse=template.fetch(entranceTest.getNextCourses());
			
			for (CourseDesignator courseDesignator2 : nextLvlCourse) {
				Set<MajorCombination> majorComb1=template.fetch(courseDesignator2.getPossibleMajors());
				for (MajorCombination majorCombination : majorComb1) {
					addNodeToGraph(jsonGraph, createNewNode(majorCombination.getAwardedCertification().name(), "D"));
					addLinkToGraph(jsonGraph, createNewLink(2, 3));
				}
			}
			
			break;
		}
		
		Set<CourseDesignator> courseDesignator1=template.fetch(majorComb.getNextCourses());
		
		for (CourseDesignator courseDesignator2 : courseDesignator1) {
			Set<MajorCombination> majorComb1=template.fetch(courseDesignator2.getPossibleMajors());
			
			for (MajorCombination majorCombination : majorComb1) {
				addNodeToGraph(jsonGraph, createNewNode(majorCombination.getAwardedCertification().name(), "D"));
				addLinkToGraph(jsonGraph, createNewLink(1, 3));
			}
		}
		
		
		
		return jsonGraph;
		
	}

	private JSONLink createNewLink(int source, int target) {
		JSONLink jsonLink = new JSONLink();
		jsonLink.setSource(source);
		jsonLink.setTarget(target);

		return jsonLink;
	}

	private JSONNode createNewNode(String name, String nodeType) {
		JSONNode jsonNode = new JSONNode();
		jsonNode.setName(name);
		jsonNode.setNodetype(nodeType);

		return jsonNode;
	}
	
	private void addNodeToGraph(JSONGraph graph,JSONNode node){
		if(graph.getNodes() ==null){
			graph.setNodes(new ArrayList<JSONNode>());
		}
		graph.getNodes().add(node);
	}

	private void addLinkToGraph(JSONGraph graph,JSONLink link){
		if(graph.getLinks() ==null){
			graph.setLinks(new ArrayList<JSONLink>());
		}
		graph.getLinks().add(link);
	}

}
